import ccclass = cc._decorator.ccclass;
import PopupWindow from "../../framework/PopupWindow";
import property = cc._decorator.property;
import {WindowName} from "../../framework/reflect/WindowName";
import Context from "../../framework/Context";
import CommonUtil from "../../framework/utils/CommonUtil";

@ccclass
@WindowName("Toast")
export default class Toast extends PopupWindow {

    public static showToast(str: string): Promise<Toast> {
        return Context.get().popup(Toast, false).then(toast => {
            toast.showToast(str);
            return Promise.resolve(toast)
        })
    }

    @property(cc.Label)
    public text: cc.Label = null;

    public onStart() {
        this.bgClickable = true
    }

    public showToast(title: string) {
        this.node.opacity = 0;
        this.text.string = title;
        this.node.runAction(cc.fadeIn(0.15));
        this.scheduleOnce(this.dismiss, 0.6)
    }

    public dismiss() {
        CommonUtil.runAction(this, cc.spawn(cc.fadeOut(0.4), cc.moveBy(0.4, 0, 100))).then(() => {
            this.finish();
        })
    }
}